<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="tong.li&#39;s blog">
  <meta name="keyword" content="彤哥哥博客，95后技术爱好者,现就职于同程旅行/同程艺龙上海分公司，专注于互联网技术分享的平台。">
  
    <link rel="shortcut icon" href="/css/images/icon.png">
  
  <title>
    
      GC调优基础之必备知识 | 彤哥哥的博客
    
  </title>
  <link href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="https://cdn.staticfile.org/highlight.js/9.12.0/styles/tomorrow-night.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
  <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.staticfile.org/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
  
  
    <!-- MathJax support START -->
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="https://cdn.staticfile.org/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <!-- MathJax support END -->
  


  
  
    
<script src="/js/local-search.js"></script>


<meta name="generator" content="Hexo 5.4.2"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>
  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>彤哥哥的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/series/" class="item-link">分类</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/project/" class="item-link">项目</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于</a>
          
        </li>
      
      
        <li class="menu-item menu-item-search right-list">
    <a role="button" class="popup-trigger">
        <i class="fa fa-search fa-fw"></i>
    </a>
</li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/series/" class="menu-link">分类</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/project/" class="menu-link">项目</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于</a>
            
          </li>
        
      </ul>
    </div>
    
      <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
            <span class="search-icon">
                <i class="fa fa-search"></i>
            </span>
            <div class="search-input-container">
                <input autocomplete="off" autocapitalize="off"
                    placeholder="Please enter your keyword(s) to search." spellcheck="false"
                    type="search" class="search-input">
            </div>
            <span class="popup-btn-close">
                <i class="fa fa-times-circle"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>
    
  </div>
</header>

    <div id="article-banner">
  <h2>GC调优基础之必备知识</h2>
  <p class="post-date">2020-05-24</p>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h1 id="1-GC基础知识"><a href="#1-GC基础知识" class="headerlink" title="1. GC基础知识"></a>1. GC基础知识</h1><h2 id="1-1-内存管理"><a href="#1-1-内存管理" class="headerlink" title="1.1 内存管理"></a>1.1 内存管理</h2><p>内存管理，百度百科的定义是：</p>
<blockquote>
<p>它指软件运行时对计算机内存资源的分配和使用的技术。</p>
<p>其最主要的目的是如何高效、快速的分配，并且在适当的时候释放和回收内存资源。</p>
</blockquote>
<p>首先，我们先来了解一下常见语言的内存管理。</p>
<p>C是通过两个标准库的函数malloc和free来完成内存空间的动态分配与释放。</p>
<p>C++通过两个运算符new和delete来完成内存空间的动态分配与释放。其实它的内部机制还是C的内存管理(malloc和free)。new可以认为malloc加构造函数(初始化)的执行，new侧重于内存分配和建立对象，而malloc只是分配一块内存。</p>
<p>由此可见，C和C++的内存管理都是我们开发人员手动控制的，免不了会出现问题：忘记回收和多次回收。</p>
<p>忘记回收就会导致内存泄露，这个很好理解。</p>
<p>而多次回收就会报错，导致系统很不稳定，一次free一个指针的时候，只是清空该指针所指的堆中的对应空间，但该指针变量在栈中的值并没有没清空，它还是指向原来分配的内存空间（但是该内存空间已经不属于该指针了，CPU随时可把该指针原来所指的空间分配给任何一个指针变量）。</p>
<p>这时，再free一次时，由于该指针已经没有堆空间与之对应了，所以编译器将会提示出错。</p>
<p><strong>当一个指针被delete后，该指针就成了野指针</strong>（不指向任何内存空间的指针我们称之为野指针，<strong>野指针所指向的地方是随机的</strong>）。当再次delete该指针时，编译器就会提示你操作非法。</p>
<p>Java的内存管理全部是自动的，即自动分配内存、释放内存，开发者无需过多参与，编程简单，也不容易出错，大大提高了开发人员的效率。</p>
<p>正是因为Java屏蔽了内存管理细节，倘若我们的Java应用出现了内存泄露(该内存空间使用完毕后未回收，在不涉及复杂数据结构的一般情况下，表现为一个内存对象的生命周期超出了程序需要它的时间长度，我们有是也将其称为“对象游离”。)</p>
<p>这个时候我们需要更深入的分析、排查和解决问题, GC实现细节对于我们开发人员将显得非常重要，因此我们Java工程师必须从更底层了解Java内存管理的细节。</p>
<h2 id="1-2-JVM常见术语"><a href="#1-2-JVM常见术语" class="headerlink" title="1.2 JVM常见术语"></a>1.2 JVM常见术语</h2><ul>
<li>并行（parallelism），指两个或者多个事件在同一时刻发生，在现代计算机中通常指多台处理器上同时处理多个任务。</li>
<li>并发（concurrency），指两个或多个事件在同一时间间隔内发生，在现代计算机中一台处理器“同时”处理多个任务，那么这些任务只能交替运行，从处理器的角度上看任务只能串行执行，从用户的角度看这些任务“并行”执行，实际上是处理器根据一定的策略不断地切换执行这些“并行”的任务。</li>
<li>JVM中的并行，指多个垃圾回收相关线程在操作系统之上并发运行，这里的并行强调的是只有垃圾回收线程工作，Java应用程序都暂停执行，因此ParNew工作的时候一定发生了STW。</li>
<li>JVM中的并发，指垃圾回收相关的线程并发运行（如果启动多个线程），同时这些线程会和Java应用程序并发运行。</li>
<li>STW（Stop-The-World）, 直译就是停止一切，在JVM中指停止一切Java应用线程。</li>
<li>安全点（Safepoint），指JVM在执行一些操作的时需要STW，但并不是任何线程在任何地方都能进入STW，例如我们正在执行一段代码时，线程如何能够停止？设计安全点的目的是，当线程进入到安全点时，线程就会主动停止。</li>
<li>Mutator，它指的是我们的Java应用线程。Mutator的含义是可变的，在这里的含义是因为线程运行，导致了内存的变化。GC中通常需要STW才能使Mutator暂停。</li>
<li>GC Root，垃圾回收的根。在JVM的垃圾回收过程中，需要从GC Root出发标记活跃对象，确保正在使用的对象在垃圾回收后都是存活的。</li>
<li>根集合（Root Set）。在JVM的垃圾回收过程中，需要从不同的GC Root出发，这些GC Root有线程栈、monitor列表、JNI对象等，而这些GC Root就构成了Root Set。</li>
<li>Full GC，简称为FGC，整个堆的垃圾回收动作。通常Full GC是串行的，G1的Full GC不仅有串行实现，在JDK10中还有并行实现。</li>
<li>Minor GC/Young GC，指目标只是新生代的垃圾收集。</li>
<li>老年代收集（Major GC/Old GC）：指目标只是老年代的垃圾收集。目前只有CMS收集器会有单独收集老年代的行为。</li>
<li>再标记（Remark）。指的是并发标记算法中，处理完并发标记后，需要更新并发标记中Mutator变更的引用，这一步需要STW。</li>
</ul>
<h2 id="1-3-垃圾定位算法"><a href="#1-3-垃圾定位算法" class="headerlink" title="1.3 垃圾定位算法"></a>1.3 垃圾定位算法</h2><h3 id="1-3-1-引用计数算法"><a href="#1-3-1-引用计数算法" class="headerlink" title="1.3.1 引用计数算法"></a>1.3.1 引用计数算法</h3><p>引用计数算法，即Reference Count, 它是垃圾收集器中的早期策略。</p>
<p>在对象中添加一个引用计数器，每当有一个地方引用它时，计数器值就+1；</p>
<p>当引用失效时，计数器值就-1；</p>
<p>任何时刻计数器为零的对象就是不可能再被使用的，会被当作垃圾收集。</p>
<p>优点: </p>
<p>原理简单，判断效率也高，可以很快的执行，交织在程序运行中。</p>
<p>对程序需要不被长时间打断的实时环境比较有利，Python的默认内存垃圾定位算法。</p>
<p>缺点：无法检测出循环引用，这种算法将会变得力不从心。因此Java并没有选用引用计数算法来管理内存</p>
<p>我们先看一段对象循环引用的代码:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@projectName</span>: ltmf</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@className</span>: com.ltmf.gc.ReferenceCountTest</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 证明Java定位垃圾不是引用计数算法</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: tong.li</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@createTime</span>: 2020/5/24 10:43:22</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span>: v1.0</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ReferenceCountTest</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 成员引用对象 */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">ReferenceCountTest</span> <span class="variable">instance</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 创建对象object1</span></span><br><span class="line">        <span class="type">ReferenceCountTest</span> <span class="variable">object1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ReferenceCountTest</span>();</span><br><span class="line">        <span class="comment">// 创建对象object2</span></span><br><span class="line">        <span class="type">ReferenceCountTest</span> <span class="variable">object2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ReferenceCountTest</span>();</span><br><span class="line">        <span class="comment">// 将object2对象的引用set到object1.instance引用变量中,使object1和object2互相引用，理论上引用计数器不可能为0，垃圾收集器就永远不会回收它们。</span></span><br><span class="line">        object1.instance = object2;</span><br><span class="line">        <span class="comment">// 将object1对象的引用set到object2.instance引用变量中,使object1和object2互相引用，理论上引用计数器不可能为0，垃圾收集器就永远不会回收它们。</span></span><br><span class="line">        object2.instance = object1;</span><br><span class="line">        <span class="comment">// 设置为object1为null，为了更快的GC，但是和object2互相引用</span></span><br><span class="line">        object1 = <span class="literal">null</span>;</span><br><span class="line">        <span class="comment">// 设置为object2为null，为了更快的GC</span></span><br><span class="line">        object2 = <span class="literal">null</span>;</span><br><span class="line">        <span class="comment">// 手动回收，调用System.gc()，先触发Minor GC，然后再触发Full GC</span></span><br><span class="line">        System.gc();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后我们在运行时候添加JVM参数设置堆大小和GC日志打印：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 设置堆的初始化大小和最大大小为50M, 并打印GC日志</span></span><br><span class="line">-Xms50M -Xmx50M -XX:+PrintGCDetails</span><br></pre></td></tr></table></figure>

<p>最后我们运行，运行结果如下:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">[GC (System.gc()) [PSYoungGen: 7027K-&gt;2021K(14848K)] 7027K-&gt;2053K(49152K), 0.0033907 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] </span><br><span class="line">[Full GC (System.gc()) [PSYoungGen: 2021K-&gt;0K(14848K)] [ParOldGen: 32K-&gt;1932K(34304K)] 2053K-&gt;1932K(49152K), [Metaspace: 3316K-&gt;3316K(1056768K)], 0.0097631 secs] [Times: user=0.03 sys=0.00, real=0.01 secs] </span><br><span class="line">Heap</span><br><span class="line"> PSYoungGen      total 14848K, used 128K [0x00000000fef80000, 0x0000000100000000, 0x0000000100000000)</span><br><span class="line">  eden space 12800K, 1% used [0x00000000fef80000,0x00000000fefa0178,0x00000000ffc00000)</span><br><span class="line">  from space 2048K, 0% used [0x00000000ffc00000,0x00000000ffc00000,0x00000000ffe00000)</span><br><span class="line">  to   space 2048K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x0000000100000000)</span><br><span class="line"> ParOldGen       total 34304K, used 1932K [0x00000000fce00000, 0x00000000fef80000, 0x00000000fef80000)</span><br><span class="line">  object space 34304K, 5% used [0x00000000fce00000,0x00000000fcfe3328,0x00000000fef80000)</span><br><span class="line"> Metaspace       used 3339K, capacity 4496K, committed 4864K, reserved 1056768K</span><br><span class="line">  class space    used 355K, capacity 388K, committed 512K, reserved 1048576K</span><br></pre></td></tr></table></figure>

<p>从上述GC日志分析出来，调用System.gc();后先触发了一次Minor GC， 随后触发了一次Full GC，</p>
<p>[PSYoungGen: 7027K-&gt;2021K(14848K)] 7027K-&gt;2053K(49152K)，先回收掉了新生代的垃圾。<br>[ParOldGen: 32K-&gt;1932K(34304K)] 2053K-&gt;1932K(49152K)。</p>
<p>由此可见，循环引用的对象也回收了，也证实了Java并不是通过引用计数算法来判断对象是否存活的。</p>
<p>接下来，我们证明一下Python循环引用对象。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/usr/bin python3.6</span></span><br><span class="line"><span class="comment"># -*- encoding: utf-8 -*-</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">@File    : __init__.py.py</span></span><br><span class="line"><span class="string">@Description : Python循环依赖问题</span></span><br><span class="line"><span class="string">@Author  : tong.li</span></span><br><span class="line"><span class="string">@Email   : lt_alex@163.com</span></span><br><span class="line"><span class="string">@Blog    : http://ltmf.top</span></span><br><span class="line"><span class="string">@Time    : 2020/05/24 下午8:31</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="comment"># 可以用来检测内存泄漏的类库</span></span><br><span class="line"><span class="keyword">import</span> objgraph</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    a = <span class="built_in">list</span>()</span><br><span class="line">    b = <span class="built_in">list</span>()</span><br><span class="line">    <span class="comment"># 查看list类型对象的数量</span></span><br><span class="line">    <span class="built_in">print</span>(objgraph.count(<span class="string">&#x27;list&#x27;</span>))</span><br><span class="line">    <span class="comment"># 循环引用</span></span><br><span class="line">    a.append(b)</span><br><span class="line">    <span class="comment"># 循环引用</span></span><br><span class="line">    b.append(a)</span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(a)</span><br><span class="line">    <span class="built_in">print</span>(b)</span><br><span class="line">    <span class="comment"># 删除对象</span></span><br><span class="line">    <span class="keyword">del</span> a</span><br><span class="line">    <span class="keyword">del</span> b</span><br><span class="line">    <span class="comment"># 再次查看list类型对象的数量</span></span><br><span class="line">    <span class="built_in">print</span>(objgraph.count(<span class="string">&#x27;list&#x27;</span>))</span><br></pre></td></tr></table></figure>

<p>执行上述结果为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 打印list类型内部的对象数</span></span><br><span class="line">158</span><br><span class="line">[[[...]]]</span><br><span class="line">[[[...]]]</span><br><span class="line"><span class="comment"># 删除对象后，list类型内部的对象数仍然为158，说明并没有回收</span></span><br><span class="line">158</span><br></pre></td></tr></table></figure>

<p>若我们在代码上注释掉循环引用的两行代码，循环引用打印结果为:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 打印list类型内部的对象数</span></span><br><span class="line">158</span><br><span class="line">[]</span><br><span class="line">[]</span><br><span class="line"><span class="comment"># del操作后，两个对象会回收，所以会变为156，此时我们可以判断确实存在循环对象引用无法回收</span></span><br><span class="line">156</span><br></pre></td></tr></table></figure>

<p>当然我们打开注释，并在最后一行代码添加一些代码：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 调用GC收集器</span></span><br><span class="line">gc.collect()</span><br></pre></td></tr></table></figure>

<p>添加并执行，我们发现 gc.collect()解决了循环引用的回收问题。由此可见，Python默认的垃圾定位算法是引用计数算法。</p>
<h3 id="1-3-2-根可达性分析算法"><a href="#1-3-2-根可达性分析算法" class="headerlink" title="1.3.2 根可达性分析算法"></a>1.3.2 根可达性分析算法</h3><p>根可达性分析算法，即Root Reachability Analysis，当前主流语言(Java、C#)都是基于根可达性分析算法来判定对象的存活。</p>
<p>根搜索算法是从离散数学中的图论引入的，它的基本思路是程序把所有的引用关系看作一张图，通过一系列称为“GC Roots”的根对象作为起始节点集，</p>
<p>从这些节点开始，根据引用关系向下搜索，搜索过程所走过的路径称为“引用链”（Reference Chain），</p>
<p>如果某个对象到GCRoots间没有任何引用链相连，或者用图论的话来说就是从GC Roots到这个对象不可达时，则证明此对象是不可能再被使用的。</p>
<p><img src="https://ltyeamin.github.io/imgs/2020/07/20200731161947.jpeg" alt="image"></p>
<p>上图表示了对象引用图，蓝色对象引用表示对象存活，</p>
<p>而橙色的ObjF、ObjE、ObjeD三个对象是GC Root不可达，我们可以断定出这三个对象是可回收的对象。</p>
<p>即使ObjD和ObjE两个对象之间有引用关系，也算垃圾对象。</p>
<p>在Java中，可作为GC Root的对象有:</p>
<ul>
<li>虚拟机栈（栈帧中的本地变量表）中引用的对象，譬如各个线程被调用的方法堆栈中使用到的参数、局部变量、临时变量等。</li>
<li>方法区中静态属性引用的对象，譬如Java类的引用类型静态变量。</li>
<li>方法区中常量引用的对象，譬如字符串常量池（String Table）里的引用。</li>
<li>本地方法栈中引用的JNI对象（Native对象）。</li>
<li>Java虚拟机内部的引用，如基本数据类型对应的Class对象，一些常驻的异常对象（比如NullPointExcepiton、OutOfMemoryError）等，还有系统类加载器。</li>
<li>所有被同步锁（synchronized关键字）持有的对象。</li>
<li>反映Java虚拟机内部情况的JMXBean、JVMTI中注册的回调、本地代码缓存等。</li>
</ul>
<p>除了以上GC Root，根据用户所选用的垃圾收集器以及当前回收的内存区域不同，还可以有其他对象“临时性”地加入，共同构成完整GC Roots集合。</p>
<p>比如分代收集和局部回收（Partial GC），如果只针对Java堆中某一块区域发起垃圾收集时（如最典型的只针对新生代的垃圾收集），必须考虑到内存区域是虚拟机自己的实现细节（在用户视角里任何内存区域都是不可见的），更不是孤立封闭的，所以某个区域里的对象完全有可能被位于堆中其他区域的对象所引用，这时候就需要将这些关联区域的对象也一并加入GC Roots集合中去，才能保证可达性分析的正确性。</p>
<p>目前最新的几款垃圾收集器（G1、Shenandoah、ZGC）无一例外都具备了局部回收的特征，为了避免GC Roots包含过多对象而过度膨胀，它们在实现上也做出了各种优化处理。</p>
<p><strong>需要注意的是，当通过根可达性分析出那些引用不可达的对象时候，只能初步断定为可回收的对象，并不是立即回收该对象</strong>，一个对象的死亡需要经历两次标记：</p>
<ol>
<li>判断标记为可回收对象。</li>
<li>标记筛选是否有必要执行finalize()方法<ol>
<li>若对象重写finalize()方法或finalize()方法已经被虚拟机调用过，那么虚拟机将这两种情况都视为“没有必要执行”。</li>
<li>若对象没有2.1的上述情况，那么该对象将会被放置在一个名为F-Queue的队列之中，并在稍后由一条由虚拟机自动建立的、低调度优先级的Finalizer线程去执行它们的finalize()方法。</li>
</ol>
</li>
</ol>
<p>2.1所说的“执行”是指虚拟机会触发这个方法开始运行，但并不承诺一定会等待它运行结束。</p>
<p>这样做的原因是，如果某个对象的finalize()方法执行缓慢，或者更极端地发生了死循环，将很可能导致F-Queue队列中的其他对象永久处于等待，甚至导致整个内存回收子系统的崩溃。</p>
<p>finalize()方法是对象逃脱死亡命运的最后一次机会，稍后收集器将对F-Queue中的对象进行第二次小规模的标记。</p>
<p>如果对象要在finalize()中成功拯救自己——只要重新与引用链上的任何一个对象建立关联即可，譬如把自己（this关键字）赋值给某个类变量或者对象的成员变量，那在第二次标记时它将被移出“即将回收”的集合；如果对象这时候还没有逃脱，那基本上它就真的要被回收了。以下代码，就是对象唯一一次的自我拯救：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@projectName</span>: ltmf</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@className</span>: com.ltmf.gc.FinalizeEscapeGc</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 对象在GC时可以自救一次，因为finalize()方法只调用一次</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: tong.li</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@createTime</span>: 2020/5/23 14:52</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span>: v1.0</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FinalizeEscapeGC</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 持有的成员变量</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">FinalizeEscapeGC</span> <span class="variable">SAVE_HOOK</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 判断是否存活的方法</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">isAlive</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;yes, i am still alive :)&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 重写finalize()，生成环境禁止重写finalize()方法</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Throwable</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">finalize</span><span class="params">()</span> <span class="keyword">throws</span> Throwable &#123;</span><br><span class="line">        <span class="built_in">super</span>.finalize();</span><br><span class="line">        System.out.println(<span class="string">&quot;finalize method executed!&quot;</span>);</span><br><span class="line">        <span class="comment">// 自己引用自己，自救</span></span><br><span class="line">        FinalizeEscapeGC.SAVE_HOOK = <span class="built_in">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Throwable &#123;</span><br><span class="line">        <span class="comment">// 实例化一个对象</span></span><br><span class="line">        SAVE_HOOK = <span class="keyword">new</span> <span class="title class_">FinalizeEscapeGC</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//对象第一次成功拯救自己</span></span><br><span class="line">        SAVE_HOOK = <span class="literal">null</span>;</span><br><span class="line">        <span class="comment">// 触发gc，但是不一定触发垃圾回收</span></span><br><span class="line">        System.gc();</span><br><span class="line">        <span class="comment">// 因为Finalizer方法优先级很低，暂停0.5秒，等待它...</span></span><br><span class="line">        Thread.sleep(<span class="number">500</span>);</span><br><span class="line">        <span class="keyword">if</span> (SAVE_HOOK != <span class="literal">null</span>) &#123;</span><br><span class="line">            SAVE_HOOK.isAlive();</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;no, i am dead :(&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 下面这段代码与上面的完全相同，但是这次自救却失败了</span></span><br><span class="line">        SAVE_HOOK = <span class="literal">null</span>;</span><br><span class="line">        System.gc();</span><br><span class="line">        <span class="comment">// 因为finalize()方法优先级很低，暂停0.5秒，等待它...</span></span><br><span class="line">        Thread.sleep(<span class="number">500</span>);...</span><br><span class="line">        <span class="keyword">if</span> (SAVE_HOOK != <span class="literal">null</span>) &#123;</span><br><span class="line">            SAVE_HOOK.isAlive();</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;no, i am dead :(&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># finalize()执行</span></span><br><span class="line">finalize method executed!</span><br><span class="line"><span class="comment"># 第一次对象自救成功</span></span><br><span class="line"><span class="built_in">yes</span>, i am still alive :)</span><br><span class="line"><span class="comment"># 第二次对象自救失败</span></span><br><span class="line">no, i am dead :(</span><br></pre></td></tr></table></figure>



<h2 id="1-4-垃圾回收算法实现"><a href="#1-4-垃圾回收算法实现" class="headerlink" title="1.4 垃圾回收算法实现"></a>1.4 垃圾回收算法实现</h2><h3 id="1-4-1-标记清除算法"><a href="#1-4-1-标记清除算法" class="headerlink" title="1.4.1 标记清除算法"></a>1.4.1 标记清除算法</h3><p>标记清除(Mark-Sweep)，最早的最基础的垃圾回收算法。</p>
<p>它分为两个阶段：标记阶段和清除阶段。从根集合出发，标记出所有需要回收的对象，在标记完成后，统一回收掉所有被标记的对象。也可以反着来。标记存活的对象，统一回收所有未被标记的对象。</p>
<p>优点：实现简单，不需要进行对象移动，在存活对象比较多的情况下极为高效。</p>
<p>缺点：一是执行效率不稳定，如果Java堆中包含大量对象，而且其中大部分是需要被回收的，这时必须进行大量标记和清除的动作，导致标记和清除两个过程的执行效率(两遍扫描)都随对象数量增长而降低；二是内存空间的碎片化问题，标记、清除之后会产生大量不连续的内存碎片，空间碎片太多可能会导致当以后在程序运行过程中需要分配较大对象时无法找到足够的连续内存而不得不提前触发另一次垃圾收集动作。</p>
<p><img src="https://ltyeamin.github.io/imgs/2020/07/20200731162141.png" alt="标记清除"></p>
<h3 id="1-4-2-复制算法"><a href="#1-4-2-复制算法" class="headerlink" title="1.4.2 复制算法"></a>1.4.2 复制算法</h3><p>标记-复制算法，我们常叫做复制(copying)算法。</p>
<p>它的实现也有很多种，可以使用两个分区，也可以使用多个分区。</p>
<p>使用两个分区时内存的利用率只有50%；使用多个分区（如3个分区），则可以提高内存的使用率。</p>
<p>工作原理：当一块的内存用完了，就将还存活着的对象复制到另外一块上面，然后再把已使用过的内存空间一次清理掉。</p>
<p>如果内存中多数对象都是存活的，这种算法将会产生大量的内存间复制的开销，但对于多数对象都是可回收的情况，算法需要复制的就是占少数的存活对象，而且每次都是针对整个半区进行内存回收，分配内存时也就不用考虑有空间碎片的复杂情况，只要移动堆顶指针，按顺序分配即可。</p>
<p>因此，我们可以总结出其的优缺点：</p>
<p>优点:实现简单，运行高效，没有内存碎片。</p>
<p>缺点：内存缩小原来的一半，空间浪费严重。在对象存活率较高时就要进行较多的复制操作，效率将会降低。</p>
<p><img src="https://ltyeamin.github.io/imgs/2020/07/20200731162149.png" alt="复制算法图示"></p>
<h3 id="1-4-3-标记压缩算法"><a href="#1-4-3-标记压缩算法" class="headerlink" title="1.4.3 标记压缩算法"></a>1.4.3 标记压缩算法</h3><p>标记压缩(mark compact)，和标记清除算法类似，在此基础上将存活的对象都向内存空间一端移动，将垃圾对象移动到内存空间的另一端，然后直接清理掉边界以外的内存，直接对可回收对象进行清理。</p>
<p>标记-清除算法与标记-整理算法的本质差异在于前者是一种非移动式的回收算法，而后者是移动式的。</p>
<p>优点：没有碎片，不需要类似复制算法的<a target="_blank" rel="noopener" href="https://cloud.tencent.com/developer/article/1082730">分配担保</a>空间。</p>
<p>缺点：移动对象，指针需要调整，两次扫描，效率偏低。</p>
<p><img src="https://ltyeamin.github.io/imgs/2020/07/20200731162155.png" alt="标记整理算法图示"></p>
<h2 id="1-5-JVM内存分代收集模型"><a href="#1-5-JVM内存分代收集模型" class="headerlink" title="1.5 JVM内存分代收集模型"></a>1.5 JVM内存分代收集模型</h2><p>分代收集（Generational Collection）就是把内存划分成不同的区域进行管理，其思想来源是：有些对象存活的时间短，有些对象存活的时间长，把存活时间短的对象放在一个区域管理，把存活时间长的对象放在另一个区域管理。</p>
<p>那么可以为两个不同的区域选择不同的算法，加快垃圾回收的效率。我们假定内存被划分成2个代：新生代和老生代。这样的组合算法，使得堆的分配率和使用率大大提高。</p>
<p><strong>把容易死亡的对象放在新生代，通常采用复制算法回收；</strong></p>
<p><strong>把预期存活时间较长的对象放在老生代，通常采用标记整理算法。</strong></p>
<p><img src="https://ltyeamin.github.io/imgs/2020/07/20200731162211.jpeg" alt="Generational Collection"></p>
<p>HotSpot虚拟机默认把新生代分为三部分：Eden区、Surivor1区、Surivor2区。三区之间的比例是8:1:1。</p>
<p>新生代发生的GC也叫做Minor GC或YGC，MinorGC/YGC发生频率比较高(不一定等Eden区满了才触发)</p>
<p>其工作机制是：每次分配内存只使用Eden和其中一块Survivor，发生垃圾收集时，将Eden和Survivor中仍然存活的对象一次性复制到另外一块Survivor空间上，然后直接清理掉Eden和已用过的那块Survivor空间。如此往复，当新生代无法分配内存时，会存在内存分配担保机制，会把新生代存活的对象转移到老年代中。</p>
<p>老年代是指在年轻代中经历了N次垃圾回收后仍然存活的对象，就会被放到年老代中。因此，可以认为年老代中存放的都是一些生命周期较长的对象。老年代内存比新生代也大很多(大概比例是1:2)，当老年代内存满时触发Major GC即Full GC，Full GC发生频率比较低，老年代对象存活时间比较长，存活率标记高。</p>
<p>除此之外，永久代（Permanent Generation） 在堆区之外还有一个代就是永久代,用于存放静态文件，如Java类、常量、方法描述等。持久代对垃圾回收没有显著影响，但是有些应用可能动态生成或者调用一些class，例如Hibernate 等，在这种时候需要设置一个比较大的持久代空间来存放这些运行过程中新增的类。对永久代的回收主要回收两部分内容：废弃常量和无用的类。JDK8移除了永久代，改为元空间。</p>
<h1 id="2-垃圾回收器"><a href="#2-垃圾回收器" class="headerlink" title="2. 垃圾回收器"></a>2. 垃圾回收器</h1><p><img src="https://ltyeamin.github.io/imgs/2020/07/20200731162254.png" alt="常见的GC垃圾回收器"></p>
<p>如果说收集算法是内存回收的方法论，那垃圾收集器就是内存回收的实践者。</p>
<p>上图就是十种常见的垃圾回收器。虚线连接的两个垃圾回收器可搭配使用。</p>
<h2 id="2-1-Serial收集器"><a href="#2-1-Serial收集器" class="headerlink" title="2.1 Serial收集器"></a>2.1 Serial收集器</h2><p>最基础、历史最悠久的单线程串行收集器，JDK1.3之前的唯一收集器,针对的是新生代垃圾回收。</p>
<p>主要针对的是几十兆甚至一两百兆的的垃圾回收，最多一百多毫秒以内的回收，运行在客户端模式下是个很好的选择，但是收集过程中一定会发生STW。</p>
<h2 id="2-2-Serial-Old收集器"><a href="#2-2-Serial-Old收集器" class="headerlink" title="2.2 Serial Old收集器"></a>2.2 Serial Old收集器</h2><p>Serial Old是Serial收集器的老年代版本，在服务器端模式下</p>
<h2 id="2-3-ParNew收集器"><a href="#2-3-ParNew收集器" class="headerlink" title="2.3 ParNew收集器"></a>2.3 ParNew收集器</h2><p>是Serial收集器多线程并行版本，多运行在服务器端，JDK7之前首选的新生代并行收集器。除Serial收集器外，目前只有它能与CMS收集器配合使用。</p>
<h2 id="2-4-CMS收集器"><a href="#2-4-CMS收集器" class="headerlink" title="2.4 CMS收集器"></a>2.4 CMS收集器</h2><p>CMS(Concurrent Mark Sweep)收集器 。划时代意义的老年代垃圾回收器，可以支持几十个G 的垃圾回收。</p>
<p>这款收集器是HotSpot虚拟机中第一款真正意义上支持并发的垃圾收集器，它首次实现了让垃圾收集线程与用户线程（基本上）同时工作。</p>
<p>美中不足的是无法与Parallel Scavenge新生代垃圾回收器配合使用。只能与ParNew或者Serial收集器中的一个配合使用。</p>
<p>CMS收集器是基于<strong>标记-清除</strong>算法实现的。主要收集回收过程为四步：初始标记、并发标记、重新标记、并发清除。</p>
<p>初始标记仅仅只是标记一下GC Roots能直接关联到的对象，速度很快；</p>
<p>并发标记阶段就是从GC Roots的直接关联对象开始遍历整个对象图的过程，这个过程耗时较长但是不需要停顿用户线程，可以与垃圾收集线程一起并发运行；</p>
<p>而重新标记阶段则是为了修正并发标记期间，因用户程序继续运作而导致标记产生变动的那一部分对象的标记记录，这个阶段的停顿时间通常会比初始标记阶段稍长一些，但也远比并发标记阶段的时间短；</p>
<p>并发清除阶段，清理删除掉标记阶段判断的已经死亡的对象，由于不需要移动存活对象，所以这个阶段也是可以与用户线程同时并发的。</p>
<p>综上所述，我们总结出初始标记、重新标记仍然需要STW。</p>
<p>CMS存在的问题是无法处理“浮动垃圾”，有可能出现“Con-current Mode Failure”失败进而导致另一次完全“Stop The World”的Full GC的产生。</p>
<p>在CMS的并发标记和并发清理阶段，用户线程是还在继续运行的，程序在运行自然就还会伴随有新的垃圾对象不断产生，但这一部分垃圾对象是出现在标记过程结束以后，CMS无法在当次收集中处理掉它们，只好留待下一次垃圾收集时再清理掉。这一部分垃圾就称为“浮动垃圾”。</p>
<p>还有一个缺点就是采用标记-清楚算法，收集结束时会有大量空间碎片产生。空间碎片过多时，将会给大对象分配带来很大麻烦，往往会出现老年代还有很多剩余空间，但就是无法找到足够大的连续空间来分配当前对象，而不得不提前触发一次Full GC的情况。</p>
<h2 id="2-5-Parallel-Scavenge收集器"><a href="#2-5-Parallel-Scavenge收集器" class="headerlink" title="2.5 Parallel Scavenge收集器"></a>2.5 Parallel Scavenge收集器</h2><p>CMS等收集器的关注点是尽可能地缩短垃圾收集时用户线程的停顿时间，而Parallel Scavenge收集器的目标则是达到一个可控制的吞吐量（Throughput）。</p>
<h2 id="2-6-Parallel-Old收集器"><a href="#2-6-Parallel-Old收集器" class="headerlink" title="2.6 Parallel Old收集器"></a>2.6 Parallel Old收集器</h2><p>Parallel Old是Parallel Scavenge收集器的老年代版本，支持多线程并发收集，基于标记-整理算法实现。JDK1.6开始提供。</p>
<p>JDK1.8默认的垃圾回收是Parallel Scavenge+Parallel Old。</p>
<h2 id="2-7-G1收集器"><a href="#2-7-G1收集器" class="headerlink" title="2.7 G1收集器"></a>2.7 G1收集器</h2><p>继承和改进了CMS收集器，G1面向全堆的收集器闪亮登场，不再需要其他新生代收集器的配合工作。</p>
<p>JDK9开始，G1宣告取代Parallel Scavenge加Parallel Old组合，成为服务端模式下的默认垃圾收集器。，而CMS则沦落至被声明为不推荐使用（Deprecate）的收集器。</p>
<p>它开创了收集器面向局部收集的设计思路和基于Region的内存布局形式。支持上百G的垃圾回收，停顿时间更小。目标是替换JDK1.5的CMS收集器。它可以指定最大停顿时间。可将停顿时间缩小在200ms - 10ms之间。</p>
<p>G1之前的所有收集器都是全堆(要么整个新生代、要么整个老年代)垃圾回收，而G1跳出了这个樊笼，它可以面向堆内存任何部分来组成回收集（Collection Set，一般简称CSet）进行回收，衡量标准不再是它属于哪个分代，而是哪块内存中存放的垃圾数量最多，回收收益最大，这就是G1收集器的Mixed GC模式。</p>
<p>G1开创的基于Region的堆内存布局是它能够实现这个目标的关键。虽然G1也仍是遵循分代收集理论设计的，但其堆内存的布局与其他收集器有非常明显的差异：G1不再坚持固定大小以及固定数量的分代区域划分，而是把连续的Java堆划分为多个大小相等的独立区域（Region），每一个Region都可以根据需要，扮演新生代的Eden空间、Survivor空间，或者老年代空间。收集器能够对扮演不同角色的Region采用不同的策略去处理，这样无论是新创建的对象还是已经存活了一段时间、熬过多次收集的旧对象都能获取很好的收集效果。</p>
<p>Region中还有一类特殊的Humongous区域，专门用来存储大对象。G1认为只要大小超过了一个Region容量一半的对象即可判定为大对象。</p>
<p>G1收集器的运作过程大致可划分为以下四个步骤：初始标记、并发标记、最终标记、筛选回收。</p>
<p>G1收集器除了并发标记外，其余阶段也是要完全暂停用户线程的，换言之，它并非纯粹地追求低延迟，官方给它设定的目标是在延迟可控的情况下获得尽可能高的吞吐量。</p>
<p>相比CMS，G1的优点有很多，<strong>可以指定最大停顿时间、分Region的内存布局、按收益动态确定回收集</strong>这些创新性设计带来的红利。</p>
<p>从整体看，G1是基于“标记-整理”算法实现的收集器，但从局部（两个Region之间）上看又是基于“标记-复制”算法实现，无论如何都不会产生内存碎片。</p>
<h2 id="2-8-ZGC收集器"><a href="#2-8-ZGC收集器" class="headerlink" title="2.8 ZGC收集器"></a>2.8 ZGC收集器</h2><p>是一款在JDK 11中新加入的具有实验性质的低延迟垃圾收集器，是由Oracle公司研发的，支持4T-16T的垃圾回收。</p>
<p>ZGC收集器是一款基于Region内存布局的，（暂时）不设分代的，使用了读屏障、染色指针和内存多重映射等技术来实现可并发的标记-整理算法的，以低延迟为首要目标的一款垃圾收集器。可将停顿时间缩小在10ms - 1ms之间。</p>
<p>回收算法使用：ColoredPointers + LoadBarrier</p>
<h1 id="3-JVM命令行参数"><a href="#3-JVM命令行参数" class="headerlink" title="3. JVM命令行参数"></a>3. JVM命令行参数</h1><p>要想JVM调优,我们首先必须熟悉JVM常用<a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html">命令行参数</a>。根据官方文档描述，HotSpot参数分为以下三类：</p>
<ul>
<li>标准： - 开头，所有的HotSpot都支持。</li>
<li>非标准：-X 开头，特定版本HotSpot支持特定命令。</li>
<li>不稳定：-XX 开头，下个版本可能取消。</li>
</ul>
<h2 id="3-1-标准参数"><a href="#3-1-标准参数" class="headerlink" title="3.1 标准参数"></a>3.1 标准参数</h2><p>当我们在命令行输入java -help显示以-开始都是标准的参数选项</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">litong@LT:~$ java -<span class="built_in">help</span></span><br><span class="line">Picked up _JAVA_OPTIONS:   -Dawt.useSystemAAFontSettings=gasp</span><br><span class="line">用法: java [-options] class [args...]</span><br><span class="line">           (执行类)</span><br><span class="line">   或  java [-options] -jar jarfile [args...]</span><br><span class="line">           (执行 jar 文件)</span><br><span class="line">其中选项包括:</span><br><span class="line">    -d32	  使用 32 位数据模型 (如果可用)</span><br><span class="line">    -d64	  使用 64 位数据模型 (如果可用)</span><br><span class="line">    -server	  选择 <span class="string">&quot;server&quot;</span> VM</span><br><span class="line">                  默认 VM 是 server,</span><br><span class="line">                  因为您是在服务器类计算机上运行。</span><br><span class="line">-<span class="built_in">cp</span> &lt;目录和 zip/jar 文件的类搜索路径&gt;</span><br><span class="line">-classpath &lt;目录和 zip/jar 文件的类搜索路径&gt;</span><br><span class="line">              用 : 分隔的目录, JAR 档案</span><br><span class="line">              和 ZIP 档案列表, 用于搜索类文件。</span><br><span class="line">-D&lt;名称&gt;=&lt;值&gt;</span><br><span class="line">              设置系统属性</span><br><span class="line">-verbose:[class|gc|jni]</span><br><span class="line">              启用详细输出</span><br><span class="line">-version      输出产品版本并退出</span><br><span class="line">-version:&lt;值&gt;</span><br><span class="line">              警告: 此功能已过时, 将在</span><br><span class="line">              未来发行版中删除。</span><br><span class="line">              需要指定的版本才能运行</span><br><span class="line">-showversion  输出产品版本并继续</span><br><span class="line">-jre-restrict-search | -no-jre-restrict-search</span><br><span class="line">              警告: 此功能已过时, 将在</span><br><span class="line">              未来发行版中删除。</span><br><span class="line">              在版本搜索中包括/排除用户专用 JRE</span><br><span class="line">-? -<span class="built_in">help</span>      输出此帮助消息</span><br><span class="line">-X            输出非标准选项的帮助</span><br><span class="line">-ea[:&lt;packagename&gt;...|:&lt;classname&gt;]</span><br><span class="line">-enableassertions[:&lt;packagename&gt;...|:&lt;classname&gt;]</span><br><span class="line">              按指定的粒度启用断言</span><br><span class="line">-da[:&lt;packagename&gt;...|:&lt;classname&gt;]</span><br><span class="line">-disableassertions[:&lt;packagename&gt;...|:&lt;classname&gt;]</span><br><span class="line">              禁用具有指定粒度的断言</span><br><span class="line">-esa | -enablesystemassertions</span><br><span class="line">              启用系统断言</span><br><span class="line">-dsa | -disablesystemassertions</span><br><span class="line">              禁用系统断言</span><br><span class="line">-agentlib:&lt;libname&gt;[=&lt;选项&gt;]</span><br><span class="line">              加载本机代理库 &lt;libname&gt;, 例如 -agentlib:hprof</span><br><span class="line">              另请参阅 -agentlib:jdwp=<span class="built_in">help</span> 和 -agentlib:hprof=<span class="built_in">help</span></span><br><span class="line">-agentpath:&lt;pathname&gt;[=&lt;选项&gt;]</span><br><span class="line">              按完整路径名加载本机代理库</span><br><span class="line">-javaagent:&lt;jarpath&gt;[=&lt;选项&gt;]</span><br><span class="line">              加载 Java 编程语言代理, 请参阅 java.lang.instrument</span><br><span class="line">-splash:&lt;imagepath&gt;</span><br><span class="line">              使用指定的图像显示启动屏幕</span><br><span class="line">有关详细信息, 请参阅 http://www.oracle.com/technetwork/java/javase/documentation/index.html。</span><br></pre></td></tr></table></figure>
<h2 id="3-2-非标准参数"><a href="#3-2-非标准参数" class="headerlink" title="3.2 非标准参数"></a>3.2 非标准参数</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">litong@LT:~$ java -X  <span class="comment"># 输出java非标准参数</span></span><br><span class="line">Picked up _JAVA_OPTIONS:   -Dawt.useSystemAAFontSettings=gasp</span><br><span class="line">    -Xmixed           混合模式执行 (默认)</span><br><span class="line">    -Xint             仅解释模式执行</span><br><span class="line">    -Xbootclasspath:&lt;用 : 分隔的目录和 zip/jar 文件&gt;</span><br><span class="line">                      设置搜索路径以引导类和资源</span><br><span class="line">    -Xbootclasspath/a:&lt;用 : 分隔的目录和 zip/jar 文件&gt;</span><br><span class="line">                      附加在引导类路径末尾</span><br><span class="line">    -Xbootclasspath/p:&lt;用 : 分隔的目录和 zip/jar 文件&gt;</span><br><span class="line">                      置于引导类路径之前</span><br><span class="line">    -Xdiag            显示附加诊断消息</span><br><span class="line">    -Xnoclassgc       禁用类垃圾收集</span><br><span class="line">    -Xincgc           启用增量垃圾收集</span><br><span class="line">    -Xloggc:&lt;file&gt;    将 GC 状态记录在文件中 (带时间戳)</span><br><span class="line">    -Xbatch           禁用后台编译</span><br><span class="line">    -Xms&lt;size&gt;        设置初始 Java 堆大小</span><br><span class="line">    -Xmx&lt;size&gt;        设置最大 Java 堆大小</span><br><span class="line">    -Xss&lt;size&gt;        设置 Java 线程堆栈大小</span><br><span class="line">    -Xprof            输出 cpu 配置文件数据</span><br><span class="line">    -Xfuture          启用最严格的检查, 预期将来的默认值</span><br><span class="line">    -Xrs              减少 Java/VM 对操作系统信号的使用 (请参阅文档)</span><br><span class="line">    -Xcheck:jni       对 JNI 函数执行其他检查</span><br><span class="line">    -Xshare:off       不尝试使用共享类数据</span><br><span class="line">    -Xshare:auto      在可能的情况下使用共享类数据 (默认)</span><br><span class="line">    -Xshare:on        要求使用共享类数据, 否则将失败。</span><br><span class="line">    -XshowSettings    显示所有设置并继续</span><br><span class="line">    -XshowSettings:all</span><br><span class="line">                      显示所有设置并继续</span><br><span class="line">    -XshowSettings:vm 显示所有与 vm 相关的设置并继续</span><br><span class="line">    -XshowSettings:properties</span><br><span class="line">                      显示所有属性设置并继续</span><br><span class="line">    -XshowSettings:locale</span><br><span class="line">                      显示所有与区域设置相关的设置并继续</span><br><span class="line"></span><br><span class="line">-X 选项是非标准选项, 如有更改, 恕不另行通知。</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="3-3-不稳定参数"><a href="#3-3-不稳定参数" class="headerlink" title="3.3 不稳定参数"></a>3.3 不稳定参数</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">litong@LT:~$ java -XX:+PrintFlagsFinal | <span class="built_in">head</span> -n 50 <span class="comment"># 这里只列举前50个不稳定参数</span></span><br><span class="line">[Global flags]</span><br><span class="line">     intx ActiveProcessorCount                      = -1                                  &#123;product&#125;</span><br><span class="line">    uintx AdaptiveSizeDecrementScaleFactor          = 4                                   &#123;product&#125;</span><br><span class="line">    uintx AdaptiveSizeMajorGCDecayTimeScale         = 10                                  &#123;product&#125;</span><br><span class="line">    uintx AdaptiveSizePausePolicy                   = 0                                   &#123;product&#125;</span><br><span class="line">    uintx AdaptiveSizePolicyCollectionCostMargin    = 50                                  &#123;product&#125;</span><br><span class="line">    uintx AdaptiveSizePolicyInitializingSteps       = 20                                  &#123;product&#125;</span><br><span class="line">    uintx AdaptiveSizePolicyOutputInterval          = 0                                   &#123;product&#125;</span><br><span class="line">    uintx AdaptiveSizePolicyWeight                  = 10                                  &#123;product&#125;</span><br><span class="line">    uintx AdaptiveSizeThroughPutPolicy              = 0                                   &#123;product&#125;</span><br><span class="line">    uintx AdaptiveTimeWeight                        = 25                                  &#123;product&#125;</span><br><span class="line">     bool AdjustConcurrency                         = <span class="literal">false</span>                               &#123;product&#125;</span><br><span class="line">     bool AggressiveHeap                            = <span class="literal">false</span>                               &#123;product&#125;</span><br><span class="line">     bool AggressiveOpts                            = <span class="literal">false</span>                               &#123;product&#125;</span><br><span class="line">     intx AliasLevel                                = 3                                   &#123;C2 product&#125;</span><br><span class="line">     bool AlignVector                               = <span class="literal">false</span>                               &#123;C2 product&#125;</span><br><span class="line">     intx AllocateInstancePrefetchLines             = 1                                   &#123;product&#125;</span><br><span class="line">     intx AllocatePrefetchDistance                  = 192                                 &#123;product&#125;</span><br><span class="line">     intx AllocatePrefetchInstr                     = 0                                   &#123;product&#125;</span><br><span class="line">     intx AllocatePrefetchLines                     = 4                                   &#123;product&#125;</span><br><span class="line">     intx AllocatePrefetchStepSize                  = 64                                  &#123;product&#125;</span><br><span class="line">     intx AllocatePrefetchStyle                     = 1                                   &#123;product&#125;</span><br><span class="line">     bool AllowJNIEnvProxy                          = <span class="literal">false</span>                               &#123;product&#125;</span><br><span class="line">     bool AllowNonVirtualCalls                      = <span class="literal">false</span>                               &#123;product&#125;</span><br><span class="line">     bool AllowParallelDefineClass                  = <span class="literal">false</span>                               &#123;product&#125;</span><br><span class="line">     bool AllowUserSignalHandlers                   = <span class="literal">false</span>                               &#123;product&#125;</span><br><span class="line">     bool AlwaysActAsServerClassMachine             = <span class="literal">false</span>                               &#123;product&#125;</span><br><span class="line">     bool AlwaysCompileLoopMethods                  = <span class="literal">false</span>                               &#123;product&#125;</span><br><span class="line">     bool AlwaysLockClassLoader                     = <span class="literal">false</span>                               &#123;product&#125;</span><br><span class="line">     bool AlwaysPreTouch                            = <span class="literal">false</span>                               &#123;product&#125;</span><br><span class="line">     bool AlwaysRestoreFPU                          = <span class="literal">false</span>                               &#123;product&#125;</span><br><span class="line">     bool AlwaysTenure                              = <span class="literal">false</span>                               &#123;product&#125;</span><br><span class="line">     bool AssertOnSuspendWaitFailure                = <span class="literal">false</span>                               &#123;product&#125;</span><br><span class="line">     bool AssumeMP                                  = <span class="literal">false</span>                               &#123;product&#125;</span><br><span class="line">     intx AutoBoxCacheMax                           = 128                                 &#123;C2 product&#125;</span><br><span class="line">    uintx AutoGCSelectPauseMillis                   = 5000                                &#123;product&#125;</span><br><span class="line">     intx BCEATraceLevel                            = 0                                   &#123;product&#125;</span><br><span class="line">     intx BackEdgeThreshold                         = 100000                              &#123;pd product&#125;</span><br><span class="line">     bool BackgroundCompilation                     = <span class="literal">true</span>                                &#123;pd product&#125;</span><br><span class="line">    uintx BaseFootPrintEstimate                     = 268435456                           &#123;product&#125;</span><br><span class="line">     intx BiasedLockingBulkRebiasThreshold          = 20                                  &#123;product&#125;</span><br><span class="line">     intx BiasedLockingBulkRevokeThreshold          = 40                                  &#123;product&#125;</span><br><span class="line">     intx BiasedLockingDecayTime                    = 25000                               &#123;product&#125;</span><br><span class="line">     intx BiasedLockingStartupDelay                 = 4000                                &#123;product&#125;</span><br><span class="line">     bool BindGCTaskThreadsToCPUs                   = <span class="literal">false</span>                               &#123;product&#125;</span><br><span class="line">     bool BlockLayoutByFrequency                    = <span class="literal">true</span>                                &#123;C2 product&#125;</span><br><span class="line">     intx BlockLayoutMinDiamondPercentage           = 20                                  &#123;C2 product&#125;</span><br><span class="line">     bool BlockLayoutRotateLoops                    = <span class="literal">true</span>                                &#123;C2 product&#125;</span><br><span class="line">     bool BranchOnRegister                          = <span class="literal">false</span>                               &#123;C2 product&#125;</span><br><span class="line">     bool BytecodeVerificationLocal                 = <span class="literal">false</span>                               &#123;product&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-4-常用参数"><a href="#3-4-常用参数" class="headerlink" title="3.4 常用参数"></a>3.4 常用参数</h2><table>
<thead>
<tr>
<th align="left">参数</th>
<th align="center">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">-Xmx 1024m</td>
<td align="center">指定堆的最大大小（以字节为单位）。此值必须是1024的倍数且大于2 MB。该<code>-Xmx</code>选项等效于<code>-XX:MaxHeapSize</code>。</td>
</tr>
<tr>
<td align="left">-Xms 1024m</td>
<td align="center">设置堆的初始大小（以字节为单位）。此值必须是1024的倍数且大于1 MB。如果未设置此选项，则初始大小将设置为为老年代和年轻代分配的大小之和。可以使用<code>-Xmn</code>选项或<code>-XX:NewSize</code>选项设置年轻代的堆的初始大小。</td>
</tr>
<tr>
<td align="left">-Xmn 256m</td>
<td align="center">设置年轻代的堆的初始大小和最大大小（以字节为单位）。堆的年轻代区域用于新对象分配使用。与在其他区域相比，在该区域执行GC的频率更高。如果年轻代的大小太小，则会执行许多次Minor GC。如果大小太大，那么将仅执行Full GC，这可能需要很长时间才能完成。Oracle建议您将年轻代的大小保持在整个堆大小的一半到四分之一之间。您可以使用<code>-XX:NewSize</code>设置年轻代的初始大小和<code>-XX:MaxNewSize</code>设置年轻代的最大尺寸。</td>
</tr>
<tr>
<td align="left">-XX:NewRatio = 2</td>
<td align="center">设置年轻代与老年代大小(不含永久代/元空间)之间的比率。默认情况下，此选项设置为2。</td>
</tr>
<tr>
<td align="left">-XX:MetaspaceSize = 11</td>
<td align="center">设置分配的类元空间的大小，该类元空间将在首次超过垃圾收集时触发垃圾收集。垃圾收集的阈值取决于使用的元数据量而增加或减少。默认大小取决于平台。</td>
</tr>
<tr>
<td align="left">-Xloggc:filename</td>
<td align="center">设置应将详细的GC事件信息重定向到该文件以进行日志记录的文件。从<code>-verbose:gc</code>每个记录的事件之前的第一个GC事件开始，写入此文件的信息与经过时间后的输出类似。如果两者都使用同一命令给出，则该<code>-Xloggc</code>选项将覆盖。<code>-verbose:gcjava</code></td>
</tr>
<tr>
<td align="left">-XX:+UseTLAB</td>
<td align="center">允许在年轻代空间中使用线程本地分配块（TLAB）。默认情况下启用此选项。要禁用TLAB，请指定<code>-XX:-UseTLAB</code>。</td>
</tr>
<tr>
<td align="left">-XX:TLABSize=512k</td>
<td align="center">设置线程本地分配缓冲区（TLAB）的初始大小（以字节为单位）。如果此选项设置为0，那么JVM将自动选择初始大小。</td>
</tr>
<tr>
<td align="left">-XX:MaxTenuringThreshold=15</td>
<td align="center">设置用于自适应GC大小调整的最大使用期限阈值(垃圾的最大年龄)。最大值为15。并行（吞吐量）收集器的默认值为15，而CMS收集器的默认值为6。</td>
</tr>
<tr>
<td align="left">-XX:+UseParallelGC</td>
<td align="center">启用并行垃圾收集器（也称为吞吐量收集器）来利用多个处理器来提高应用程序的性能。默认情况下，此选项是禁用的。若-XX:+UseParallelOldGC启用，默认-XX:+UseParallelGC选项也会启用，除非你手动禁用该选项。选择的是Parallel Scavenge + Parallel Old/SerialOld垃圾回收器</td>
</tr>
<tr>
<td align="left">-XX:+UseParallelOldGC</td>
<td align="center">允许将并行垃圾收集器用于Full GC,即老年代。默认情况下，此选项是禁用的。启用它会自动启用该<code>-XX:+UseParallelGC</code>选项。选择的是Parallel Scavenge + Parallel Old垃圾回收器</td>
</tr>
<tr>
<td align="left">-XX:+UseParNewGC</td>
<td align="center">允许在年轻代中使用并行线程进行收集。默认情况下，此选项是禁用的。设置该<code>-XX:+UseConcMarkSweepGC</code>选项时，它将自动启用。JDK|8启用了-XX:+UseParNewGC+-XX:+UseConcMarkSweepGC的选择。</td>
</tr>
<tr>
<td align="left">-XX:+ UseSerialGC</td>
<td align="center">启用串行垃圾收集器的使用。对于不需要垃圾回收具有任何特殊功能的小型和简单应用程序，这通常是最佳选择。默认情况下，此选项是禁用的，并且将根据计算机的配置和JVM的类型自动选择收集器。</td>
</tr>
<tr>
<td align="left">-XX:+UseConcMarkSweepGC</td>
<td align="center">启用CMS用于老年代的垃圾收集器。当吞吐量（<code>-XX:+UseParallelGC</code>）垃圾收集器无法满足应用程序延迟要求时，Oracle建议您使用CMS垃圾收集器。G1垃圾收集器（<code>-XX:+UseG1GC</code>）是另一种选择。默认情况下，此选项是禁用的，并且将根据计算机的配置和JVM的类型自动选择收集器。启用此选项后，将<code>-XX:+UseParNewGC</code>自动设置该选项，并且您不应禁用它，因为JDK 8中已弃用以下选项组合：<code>-XX:+UseConcMarkSweepGC -XX:-UseParNewGC</code>。</td>
</tr>
<tr>
<td align="left">-XX:+UseG1GC</td>
<td align="center">启用垃圾优先（G1）垃圾收集器的使用。它是服务器类型的垃圾收集器，适用于具有大量RAM的多处理器计算机。它极有可能满足GC暂停时间目标，同时保持良好的吞吐量。建议将G1收集器用于需要大堆（大小约为6 GB或更大）且GC延迟要求有限（稳定且可预测的暂停时间低于0.5秒）的应用程序。</td>
</tr>
<tr>
<td align="left">-XX:MaxGCPauseMillis=500</td>
<td align="center">设置最大GC暂停时间的目标（以毫秒为单位）。这是一个软目标，并且JVM将尽最大的努力来实现它。默认情况下，没有最大暂停时间值。</td>
</tr>
<tr>
<td align="left">-XX:InitialSurvivorRatio=4</td>
<td align="center">设置吞吐量优先的垃圾收集器使用的初始survivor空间比率（由<code>-XX:+UseParallelGC</code>和/或- <code>XX:+UseParallelOldGC</code>选项启用）。缺省情况下，吞吐量垃圾收集器通过使用<code>-XX:+UseParallelGC</code>和<code>-XX:+UseParallelOldGC</code>选项来启用自适应大小调整，并根据应用程序的行为从初始值开始调整幸存空间的大小。如果禁用了自适应大小调整（使用该<code>-XX:-UseAdaptiveSizePolicy</code>选项），<code>-XX:SurvivorRatio</code>则应使用该选项来设置整个应用程序执行过程中surivor空间的大小。以下公式可用于根据年轻代（Y）的大小和初始survivor空间比率（R）计算幸存者空间的初始大小（S）：S = Y /（R + 2），等式中的2表示两个survivor空间。指定为初始survivor空间比率的值越大，初始幸存者空间大小越小。默认情况下，初始survivor空间比率设置为8。如果使用了年轻代空间大小的默认值（2 MB），则survivor空间的初始大小将为0.2 MB。</td>
</tr>
<tr>
<td align="left">-XX:+ UseAdaptiveSizePolicy</td>
<td align="center">用自适应生成大小调整。默认情况下启用此选项。要禁用自适应生成大小调整，<code>-XX:-UseAdaptiveSizePolicy</code>请显式指定并设置内存分配池的大小（请参阅该<code>-XX:SurvivorRatio</code>选项）。</td>
</tr>
<tr>
<td align="left">-XX:SurvivorRatio=8</td>
<td align="center">设置eden空间大小与surivor空间大小之间的比率。默认情况下，此选项设置为8。</td>
</tr>
<tr>
<td align="left">-XX:TargetSurvivorRatio=30</td>
<td align="center">设置垃圾回收后所需的surivor空间百分比（0到100）。默认情况下，此选项设置为50％。</td>
</tr>
<tr>
<td align="left">-XX:ConcGCThreads=2</td>
<td align="center">设置用于并发GC的线程数。缺省值取决于JVM可用的CPU数量。</td>
</tr>
<tr>
<td align="left">-XX:+DisableExplicitGC</td>
<td align="center">禁用对<code>System.gc()</code>的调用，但jvm在必要时仍会执行GC。</td>
</tr>
<tr>
<td align="left">-XX:+ScavengeBeforeFullGC</td>
<td align="center">在每个Full GC之前启用一次YGC。默认情况下启用此选项。Oracle建议您<em>不要</em>禁用它，因为在Full GC之前清除年轻代可以减少从老年代空间到年轻代空间可访问的对象数量。禁用的话，请指定<code>-XX:-ScavengeBeforeFullGC</code>。</td>
</tr>
<tr>
<td align="left">-XX:+HeapDumpOnOutOfMemoryError</td>
<td align="center">Java.lang.OutOfMemoryError引发异常时，使用堆分析器（HPROF）启用将Java堆转储到当前目录中的文件的功能。您可以使用该-XX:HeapDumpPath选项显式设置堆转储文件的路径和名称。默认情况下，禁用此选项，并且在OutOfMemoryError引发异常时不转储堆。</td>
</tr>
<tr>
<td align="left">-XX:HeapDumpPath=<em>path</em></td>
<td align="center">置-XX:+HeapDumpOnOutOfMemoryError选项时，设置用于写入由堆分析器（HPROF）提供的堆转储的路径和文件名。默认情况下，在当前工作目录中创建该文件，并将其命名为java_pidpid.hprof，其中pid是导致错误的进程的标识符。下面的示例演示如何显式设置默认文件（%p代表当前进程标识符）：-XX:HeapDumpPath=./java_pid%p.hprof</td>
</tr>
<tr>
<td align="left">-XX:LogFile=<em>path</em></td>
<td align="center">设置写入日志数据的路径和文件名。默认情况下，该文件在当前工作目录中创建，并且名为hotspot.log。</td>
</tr>
<tr>
<td align="left">-XX:+PrintConcurrentLocks</td>
<td align="center">启用java.util.concurrent在Control+C事件（SIGTERM）之后打印锁。默认情况下，此选项是禁用的。设置此选项等效于运行jstack -l命令或jcmd pid Thread.print -l命令，其中pid是当前Java进程标识符。</td>
</tr>
<tr>
<td align="left">-XX:+PrintGCDetails</td>
<td align="center">启用在每个GC上打印详细信息的功能。默认情况下，此选项是禁用的。</td>
</tr>
<tr>
<td align="left">-XX:-UseCompressedOops</td>
<td align="center">禁用压缩指针(普通对象指针压缩)的使用。默认情况下，此选项处于启用状态，并且当Java堆大小小于32 GB时，将使用压缩指针。启用此选项后，对象引用将表示为32位偏移量而不是64位指针，这通常在运行Java堆大小小于32 GB的应用程序时提高性能。此选项仅适用于64位JVM。</td>
</tr>
<tr>
<td align="left">-XX:+UseCompressedClassPointers</td>
<td align="center">类指针压缩，对象头的class Point部分的压缩。</td>
</tr>
<tr>
<td align="left">-XX:+ PrintCommandLineFlags</td>
<td align="center">当Java堆大小大于32GB时，也可以使用压缩指针。参见<code>-XX:ObjectAlignmentInBytes</code>选项。java -XX:+PrintCommandLineFlags -version可打印出JDK8的默认启用参数。</td>
</tr>
<tr>
<td align="left">-XX:+PrintFlagsFinal</td>
<td align="center">打印JVM中-XX的最终的参数列，默认不启用。</td>
</tr>
<tr>
<td align="left">-XX:+PrintFlagsInitial</td>
<td align="center">打印默认参数值</td>
</tr>
</tbody></table>
<h1 id="4-调优工具使用"><a href="#4-调优工具使用" class="headerlink" title="4. 调优工具使用"></a>4. 调优工具使用</h1><h2 id="4-1-JDK自带"><a href="#4-1-JDK自带" class="headerlink" title="4.1 JDK自带"></a>4.1 JDK自带</h2><p>JDK为我们自带的一些JVM故障与诊断工具，我们可以在<a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot">Oracle官方故障排除指南</a>上查询相关的资料。</p>
<h3 id="4-1-1-jps命令"><a href="#4-1-1-jps命令" class="headerlink" title="4.1.1 jps命令"></a>4.1.1 jps命令</h3><p>Jps（JVM Process Status Tool），虚拟机进程状况工具。</p>
<p>它可以列出正在运行的虚拟机进程，并显示虚拟机执行主类（Main Class，main()函数所在的类）名称以及这些进程的本地虚拟机唯一ID（LVMID，Local Virtual Machine Identifier）。</p>
<p>对于本地虚拟机进程来说，LVMID与操作系统的进程ID（PID，Process Identifier）是一致的。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 虚拟机进程状况工具</span></span><br><span class="line">litong@LT:/media/litong/软件/programs/tools/arthas$ jps -l <span class="comment"># -l 输出主类的全名，若执行的是jar包，输出jar包的路径</span></span><br><span class="line">1909 arthas-demo.jar</span><br><span class="line">16058 sun.tools.jps.Jps</span><br><span class="line">litong@LT:/media/litong/软件/programs/tools/arthas$ jps -q <span class="comment"># -q 只输出LVMID,和jps不带选项参数结果一致</span></span><br><span class="line">1909</span><br><span class="line">21372</span><br><span class="line">litong@LT:/media/litong/软件/programs/tools/arthas$ jps -v <span class="comment"># -v 输出虚拟机进程启动时的JVM参数</span></span><br><span class="line">30201 Jps -Dapplication.home=/media/litong/软件/programs/tools/jdk/jdk1.8.0_241 -Xms8m -Dawt.useSystemAAFontSettings=gasp</span><br><span class="line">litong@LT:/media/litong/软件/programs/tools/arthas$ jps -m <span class="comment"># -m 输出虚拟机进程中启动时传递给main()的参数</span></span><br><span class="line">1909 jar</span><br><span class="line">5032 Jps -m  </span><br></pre></td></tr></table></figure>

<h3 id="4-1-2-jstat命令"><a href="#4-1-2-jstat命令" class="headerlink" title="4.1.2 jstat命令"></a>4.1.2 jstat命令</h3><p>jstat（JVM Statistics Monitoring Tool）是用于监视虚拟机各种运行状态信息的命令行工具。</p>
<p>它可以显示本地或者远程[插图]虚拟机进程中的类加载、内存、垃圾收集、即时编译等运行时数据，在没有GUI图形界面、只提供了纯文本控制台环境的服务器上，它将是运行期定位虚拟机性能问题的常用工具。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">litong@LT:~$ jstat -gc 1909 1000 2 <span class="comment"># 查询Java进程为1909的gc情况(每秒查询,总共查两次,后面两个参数没有，默认只查询当前一次gc情况)</span></span><br><span class="line"> S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT   </span><br><span class="line">7680.0 7680.0  0.0    0.0   48640.0   5837.4   128512.0     0.0     4480.0 780.9  384.0   76.6       0    0.000   0      0.000    0.000</span><br><span class="line">7680.0 7680.0  0.0    0.0   48640.0   5837.4   128512.0     0.0     4480.0 780.9  384.0   76.6       0    0.000   0      0.000    0.000</span><br><span class="line">litong@LT:~$ jstat -gcutil 1909 1000 2 <span class="comment"># 与-gc内容相似,区别在于这里显示的是已使用空间占总空间的百分比</span></span><br><span class="line">  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   </span><br><span class="line">  0.00   0.00  12.00   0.00  17.43  19.94      0    0.000     0    0.000    0.000</span><br><span class="line">  0.00   0.00  12.00   0.00  17.43  19.94      0    0.000     0    0.000    0.000</span><br><span class="line">litong@LT:~$ jstat -gccause 1909 <span class="comment"># 与-gcutil内容类似，只不过多输出一列：上一次垃圾回收的原因</span></span><br><span class="line">  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT    LGCC                 GCC                 </span><br><span class="line">  0.00   0.00  14.00   0.00  17.43  19.94      0    0.000     0    0.000    0.000 No GC                No GC</span><br><span class="line">litong@LT:~$ jstat -gccapacity 1909 <span class="comment"># 与-gc内容相似，区别在于输出主要关注Java堆各个区域使用的最大、最小空间</span></span><br><span class="line"> NGCMN    NGCMX     NGC     S0C   S1C       EC      OGCMN      OGCMX       OGC         OC       MCMN     MCMX      MC     CCSMN    CCSMX     CCSC    YGC    FGC </span><br><span class="line"> 64000.0 1017856.0  64000.0 7680.0 7680.0  48640.0   128512.0  2035712.0   128512.0   128512.0      0.0 1056768.0   4480.0      0.0 1048576.0    384.0      0     0</span><br><span class="line">litong@LT:~$ jstat -class 1909 <span class="comment"># 监视类加载、卸载数量、总空间以及类装载所耗费的时间</span></span><br><span class="line">Loaded  Bytes  Unloaded  Bytes     Time   </span><br><span class="line">   554  1118.9        0     0.0       0.71</span><br><span class="line">litong@LT:~$ jstat -gcnew 1909 <span class="comment"># 监视新生代的垃圾回收情况</span></span><br><span class="line"> S0C    S1C    S0U    S1U   TT MTT  DSS      EC       EU     YGC     YGCT  </span><br><span class="line">7680.0 7680.0    0.0    0.0 15  15    0.0  48640.0   6810.2      0    0.000</span><br><span class="line">litong@LT:~$ jstat -gcnewcapacity 1909 <span class="comment"># 与-gcnew内容类似,区别在于输出主要关注新生代各个区域使用的最大、最小空间</span></span><br><span class="line">  NGCMN      NGCMX       NGC      S0CMX     S0C     S1CMX     S1C       ECMX        EC      YGC   FGC </span><br><span class="line">   64000.0  1017856.0    64000.0 338944.0   7680.0 338944.0   7680.0  1016832.0    48640.0     0     0</span><br><span class="line"> litong@LT:~$ jstat -gcold 1909 <span class="comment"># 监视老年代的垃圾回收情况</span></span><br><span class="line">   MC       MU      CCSC     CCSU       OC          OU       YGC    FGC    FGCT     GCT   </span><br><span class="line">  4480.0    780.9    384.0     76.6    128512.0         0.0      0     0    0.000    0.000</span><br><span class="line"> litong@LT:~$ jstat -gcoldcapacity 1909 <span class="comment"># 与-gcold内容类似,区别在于输出主要关注新生代各个区域使用的最大、最小空间</span></span><br><span class="line">   OGCMN       OGCMX        OGC         OC       YGC   FGC    FGCT     GCT   </span><br><span class="line">   128512.0   2035712.0    128512.0    128512.0     0     0    0.000    0.000</span><br><span class="line"> litong@LT:~$ jstat -compiler 1909 <span class="comment"># 输出即时编译器编译过的方法、耗时等信息</span></span><br><span class="line">   Compiled Failed Invalid   Time   FailedType FailedMethod</span><br><span class="line">   290      0       0     0.30          0 </span><br><span class="line"> litong@LT:~$ jstat -printcompilation 1909 <span class="comment"># 输出已经被即时编译的方法</span></span><br><span class="line">    Compiled  Size  Type Method</span><br><span class="line">    290     25    1 java/util/ArrayList<span class="variable">$Itr</span> hasNext</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<table>
<thead>
<tr>
<th>缩写符</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>E、EC、EU</td>
<td>新生代Eden区，新生代中Eden的容量 (字节)，新生代中Eden目前已使用空间 (字节)</td>
</tr>
<tr>
<td>S0、S0C、S0U</td>
<td>新生代Survivor0区，新生代第1个Survivor区的容量 (字节)，新生代第1个Survivor区目前已使用空间的容量 (字节)</td>
</tr>
<tr>
<td>S1、S1C、S1U</td>
<td>新生代Survivor1区，新生代第2个Survivor区的容量 (字节)，新生代第2个Survivor区目前已使用空间的容量 (字节)</td>
</tr>
<tr>
<td>O、OC、OU</td>
<td>老年代区以及老年代的容量和目前使用的容量(字节)</td>
</tr>
<tr>
<td>M、MC、MU</td>
<td>元空间区以及元空间的容量和目前使用的容量(字节)</td>
</tr>
<tr>
<td>CCS、CCSC、CCSU</td>
<td>类指针压缩空间以及类指针压缩空间的容量和目前使用的容量(字节)</td>
</tr>
<tr>
<td>YGC</td>
<td>Young GC，新生代GC次数</td>
</tr>
<tr>
<td>YGCT</td>
<td>Young GC Time，新生代GC总时长(毫秒)</td>
</tr>
<tr>
<td>FGC</td>
<td>Full GC回收次数</td>
</tr>
<tr>
<td>FGCT</td>
<td>Full GC Time，Full GC总时长(毫秒)</td>
</tr>
<tr>
<td>GCT</td>
<td>GC Time,所有GC总耗时(毫秒)</td>
</tr>
<tr>
<td>GCC、LGCC</td>
<td>GC Cause，当前GC的原因；Last GC Cause，上一次GC的原因；</td>
</tr>
</tbody></table>
<h3 id="4-1-3-jmap命令"><a href="#4-1-3-jmap命令" class="headerlink" title="4.1.3 jmap命令"></a>4.1.3 jmap命令</h3><p>jmap（Memory Map for Java）命令用于生成堆转储快照（一般称为heapdump或dump文件）。</p>
<p>不使用jmap命令，我们可以使用-XX：+HeapDumpOnOutOfMemoryError参数，可以让虚拟机在内存溢出异常出现之后自动生成堆转储快照文件，通过-XX：+HeapDumpOnCtrlBreak参数则可以使用[Ctrl]+[Break]键让虚拟机生成堆转储快照文件。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">litong@LT:~$ jmap -dump:format=b,file=arthas.dump 1909 <span class="comment"># 生成此时的dump堆转存储文件</span></span><br><span class="line">Dumping heap to /home/litong/arthas.dump ...</span><br><span class="line">Heap dump file created</span><br></pre></td></tr></table></figure>



<h3 id="4-1-4-jinfo命令"><a href="#4-1-4-jinfo命令" class="headerlink" title="4.1.4 jinfo命令"></a>4.1.4 jinfo命令</h3><p>jinfo（Configuration Info for Java）的作用是实时查看和调整虚拟机各项参数。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">litong@LT:~$ jinfo -flag MaxHeapSize 1909 <span class="comment"># 查看1909这个JVM进程的最大堆大小</span></span><br><span class="line">-XX:MaxHeapSize=3126853632</span><br><span class="line">litong@LT:~$ jmap -histo 1909 | <span class="built_in">head</span> -20 <span class="comment"># 显示堆中对象统计信息，包括类、实例数量、合计容量</span></span><br><span class="line"> num     <span class="comment">#instances         #bytes  class name</span></span><br><span class="line">----------------------------------------------</span><br><span class="line">   1:         63705        3518368  [C</span><br><span class="line">   2:          7790        3014168  [I</span><br><span class="line">   3:         36707         880968  java.lang.String</span><br><span class="line">   4:         12870         810888  [Ljava.lang.Object;</span><br><span class="line">   5:          9508         456384  java.nio.HeapCharBuffer</span><br><span class="line">   6:          4828         386240  [S</span><br><span class="line">   7:         14565         349560  java.lang.StringBuilder</span><br><span class="line">   8:           411         177088  [B</span><br><span class="line">   9:          2397         153408  java.text.DecimalFormatSymbols</span><br><span class="line">  10:          2397         153408  java.util.regex.Matcher</span><br><span class="line">  11:          4794         115056  java.util.Formatter<span class="variable">$FixedString</span></span><br><span class="line">  12:          4754         114096  java.util.ArrayList</span><br><span class="line">  13:          2397          95880  java.util.Formatter<span class="variable">$FormatSpecifier</span></span><br><span class="line">  14:          2397          76704  [Ljava.util.Formatter<span class="variable">$FormatString</span>;</span><br><span class="line">  15:          2397          76704  java.lang.IllegalArgumentException</span><br><span class="line">  16:          2397          76704  java.util.Formatter</span><br><span class="line">  17:          2345          75040  java.util.ArrayList<span class="variable">$Itr</span></span><br></pre></td></tr></table></figure>



<h3 id="4-1-5-jhat命令"><a href="#4-1-5-jhat命令" class="headerlink" title="4.1.5 jhat命令"></a>4.1.5 jhat命令</h3><p>JDK提供jhat（JVM Heap Analysis Tool）命令与jmap搭配使用，来分析jmap生成的堆转储快照。</p>
<p>jhat内置了一个微型的HTTP/Web服务器，生成堆转储快照的分析结果后，可以在浏览器中查看。</p>
<p>实际用的不多，主要原因有两个方面：</p>
<p>一般不会在部署应用程序的服务器上直接分析堆转储快照，即使可以这样做，也会尽量将堆转储快照文件复制到其他机器上进行分析，因为分析工作是一个耗时而且极为耗费硬件资源的过程，既然都要在其他机器上进行，就没有必要再受命令行工具的限制了。</p>
<p>jhat的分析功能相当的简陋,相比其他的分析工具VisualVM以及专业用于分析堆转储快照文件的Eclipse MemoryAnalyzer、IBM HeapAnalyzer没有竞争力。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">litong@LT:~$ jhat ./arthas.dump  <span class="comment"># 在线分析dump文件,在浏览器打开http://localhost:7000即可分析</span></span><br><span class="line">Reading from ./arthas.dump...</span><br><span class="line">Dump file created Wed May 24 22:19:29 CST 2020</span><br><span class="line">Snapshot <span class="built_in">read</span>, resolving...</span><br><span class="line">Resolving 186238 objects...</span><br><span class="line">Chasing references, expect 37 dots.....................................</span><br><span class="line">Eliminating duplicate references.....................................</span><br><span class="line">Snapshot resolved.</span><br><span class="line">Started HTTP server on port 7000</span><br><span class="line">Server is ready.</span><br></pre></td></tr></table></figure>

<h3 id="4-1-6-jstack命令"><a href="#4-1-6-jstack命令" class="headerlink" title="4.1.6 jstack命令"></a>4.1.6 jstack命令</h3><p>jstack（Stack Trace for Java）命令用于生成虚拟机当前时刻的线程快照（一般称为threaddump或者javacore文件）。</p>
<p>线程快照就是当前虚拟机内每一条线程正在执行的方法堆栈的集合，生成线程快照的目的通常是定位线程出现长时间停顿的原因，如线程间死锁、死循环、请求外部资源导致的长时间挂起等，都是导致线程长时间停顿的常见原因。线程出现停顿时通过jstack来查看各个线程的调用堆栈，就可以获知没有响应的线程到底在后台做些什么事情，或者等待着什么资源。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line">itong@LT:~$ jstack -l 1909 <span class="comment"># 除过堆栈外，显示关于所的附加信息</span></span><br><span class="line">2020-05-24 22:37:09</span><br><span class="line">Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.241-b07 mixed mode):</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;Attach Listener&quot;</span> <span class="comment">#9 daemon prio=9 os_prio=0 tid=0x00007f7db0001000 nid=0x77e waiting on condition [0x0000000000000000]</span></span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line"></span><br><span class="line">   Locked ownable synchronizers:</span><br><span class="line">	- None</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;Service Thread&quot;</span> <span class="comment">#8 daemon prio=9 os_prio=0 tid=0x00007f7dec19b000 nid=0x837 runnable [0x0000000000000000]</span></span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line"></span><br><span class="line">   Locked ownable synchronizers:</span><br><span class="line">	- None</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;C1 CompilerThread2&quot;</span> <span class="comment">#7 daemon prio=9 os_prio=0 tid=0x00007f7dec185800 nid=0x836 waiting on condition [0x0000000000000000]</span></span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line"></span><br><span class="line">   Locked ownable synchronizers:</span><br><span class="line">	- None</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;C2 CompilerThread1&quot;</span> <span class="comment">#6 daemon prio=9 os_prio=0 tid=0x00007f7dec184000 nid=0x835 waiting on condition [0x0000000000000000]</span></span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line"></span><br><span class="line">   Locked ownable synchronizers:</span><br><span class="line">	- None</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;C2 CompilerThread0&quot;</span> <span class="comment">#5 daemon prio=9 os_prio=0 tid=0x00007f7dec181000 nid=0x834 waiting on condition [0x0000000000000000]</span></span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line"></span><br><span class="line">   Locked ownable synchronizers:</span><br><span class="line">	- None</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;Signal Dispatcher&quot;</span> <span class="comment">#4 daemon prio=9 os_prio=0 tid=0x00007f7dec17f800 nid=0x833 runnable [0x0000000000000000]</span></span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line"></span><br><span class="line">   Locked ownable synchronizers:</span><br><span class="line">	- None</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;Finalizer&quot;</span> <span class="comment">#3 daemon prio=8 os_prio=0 tid=0x00007f7dec14f000 nid=0x832 in Object.wait() [0x00007f7ddc6f5000]</span></span><br><span class="line">   java.lang.Thread.State: WAITING (on object monitor)</span><br><span class="line">	at java.lang.Object.<span class="built_in">wait</span>(Native Method)</span><br><span class="line">	- waiting on &lt;0x0000000781e08ee0&gt; (a java.lang.ref.ReferenceQueue<span class="variable">$Lock</span>)</span><br><span class="line">	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)</span><br><span class="line">	- locked &lt;0x0000000781e08ee0&gt; (a java.lang.ref.ReferenceQueue<span class="variable">$Lock</span>)</span><br><span class="line">	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)</span><br><span class="line">	at java.lang.ref.Finalizer<span class="variable">$FinalizerThread</span>.run(Finalizer.java:216)</span><br><span class="line"></span><br><span class="line">   Locked ownable synchronizers:</span><br><span class="line">	- None</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;Reference Handler&quot;</span> <span class="comment">#2 daemon prio=10 os_prio=0 tid=0x00007f7dec14a800 nid=0x831 in Object.wait() [0x00007f7ddc7f6000]</span></span><br><span class="line">   java.lang.Thread.State: WAITING (on object monitor)</span><br><span class="line">	at java.lang.Object.<span class="built_in">wait</span>(Native Method)</span><br><span class="line">	- waiting on &lt;0x0000000781e06c00&gt; (a java.lang.ref.Reference<span class="variable">$Lock</span>)</span><br><span class="line">	at java.lang.Object.<span class="built_in">wait</span>(Object.java:502)</span><br><span class="line">	at java.lang.ref.Reference.tryHandlePending(Reference.java:191)</span><br><span class="line">	- locked &lt;0x0000000781e06c00&gt; (a java.lang.ref.Reference<span class="variable">$Lock</span>)</span><br><span class="line">	at java.lang.ref.Reference<span class="variable">$ReferenceHandler</span>.run(Reference.java:153)</span><br><span class="line"></span><br><span class="line">   Locked ownable synchronizers:</span><br><span class="line">	- None</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;main&quot;</span> <span class="comment">#1 prio=5 os_prio=0 tid=0x00007f7dec00b800 nid=0x81e waiting on condition [0x00007f7df5ac1000]</span></span><br><span class="line">   java.lang.Thread.State: TIMED_WAITING (sleeping)</span><br><span class="line">	at java.lang.Thread.<span class="built_in">sleep</span>(Native Method)</span><br><span class="line">	at java.lang.Thread.<span class="built_in">sleep</span>(Thread.java:340)</span><br><span class="line">	at java.util.concurrent.TimeUnit.<span class="built_in">sleep</span>(TimeUnit.java:386)</span><br><span class="line">	at demo.MathGame.main(MathGame.java:17)</span><br><span class="line"></span><br><span class="line">   Locked ownable synchronizers:</span><br><span class="line">	- None</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;VM Thread&quot;</span> os_prio=0 tid=0x00007f7dec140800 nid=0x830 runnable </span><br><span class="line"></span><br><span class="line"><span class="string">&quot;GC task thread#0 (ParallelGC)&quot;</span> os_prio=0 tid=0x00007f7dec021000 nid=0x82c runnable </span><br><span class="line"></span><br><span class="line"><span class="string">&quot;GC task thread#1 (ParallelGC)&quot;</span> os_prio=0 tid=0x00007f7dec022800 nid=0x82d runnable </span><br><span class="line"></span><br><span class="line"><span class="string">&quot;GC task thread#2 (ParallelGC)&quot;</span> os_prio=0 tid=0x00007f7dec024800 nid=0x82e runnable </span><br><span class="line"></span><br><span class="line"><span class="string">&quot;GC task thread#3 (ParallelGC)&quot;</span> os_prio=0 tid=0x00007f7dec026000 nid=0x82f runnable </span><br><span class="line"></span><br><span class="line"><span class="string">&quot;VM Periodic Task Thread&quot;</span> os_prio=0 tid=0x00007f7dec1a5800 nid=0x838 waiting on condition </span><br><span class="line"></span><br><span class="line">JNI global references: 5</span><br></pre></td></tr></table></figure>

<h3 id="4-1-7-jconsole命令"><a href="#4-1-7-jconsole命令" class="headerlink" title="4.1.7 jconsole命令"></a>4.1.7 jconsole命令</h3><p>图形化上述命令的结果，可以试用学习，生产环境不建议使用。</p>
<h3 id="4-1-8-jvisualvm命令"><a href="#4-1-8-jvisualvm命令" class="headerlink" title="4.1.8 jvisualvm命令"></a>4.1.8 jvisualvm命令</h3><p>与jcomsole类似，是jconsole高级版本,可以试用学习，生产环境不建议使用。</p>
<h2 id="4-2-Arthas"><a href="#4-2-Arthas" class="headerlink" title="4.2 Arthas"></a>4.2 Arthas</h2><p> Arthas是阿里开源的Java诊断工具。在这里就不多详讲，读者请<a target="_blank" rel="noopener" href="https://alibaba.github.io/arthas/">移步这里</a>先行学习，后面我会单独写一篇Arthas入门指南。</p>
<h2 id="4-3-JProfiler"><a href="#4-3-JProfiler" class="headerlink" title="4.3 JProfiler"></a>4.3 JProfiler</h2><p> JProfiler直观的用户界面可帮助您解决性能瓶颈，查明内存泄漏并了解线程问题。但是遗憾的是jprofiler是收费的。如果想要学习请<a target="_blank" rel="noopener" href="https://www.ej-technologies.com/products/jprofiler/overview.html">移步这里</a>。</p>
<p>相比JProfiler，我们更愿意推荐使用阿里开源的Arthas性能排查工具。</p>
<h1 id="5-面试连环炮"><a href="#5-面试连环炮" class="headerlink" title="5. 面试连环炮"></a>5. 面试连环炮</h1><ul>
<li><p>内存泄露和内存溢出的区别，怎么判断内存泄露</p>
</li>
<li><p>老年代与年轻代、元空间划分比例</p>
</li>
<li><p>多大的对象会直接被分配到老年代里</p>
</li>
<li><p>JVM堆内存管理(对象分配过程)</p>
</li>
<li><p>对象在内存中的布局</p>
<p>在HotSpot虚拟机中，对象在内存中的布局分为三部分：对象头、实例数据、对齐填充。</p>
<ol>
<li>对象头：包括两部分：Mark Word、类型指针、数组长度(如果是数组类型)<ol>
<li>Mark Word：存储对象自身的运行时数据（哈希码、GC年龄、锁标志、持有的锁等）。被设计成非固定数据结构，根据对象状态占用内部空间。</li>
<li>Class Poniter：对象指向它的类元数据的指针。虚拟机通过这个指针确定对象是哪个类的实例。</li>
<li>length：数组长度，对于数组对象，对象头中必须有一块数据记录数组长度，因为JVM无法从数组的元数据确定数组的大小。</li>
</ol>
</li>
<li> Instance Data：实例数据是对象真正存储的有效信息，就是代码中定义的各种类型的字段内容，包括从父类继承下来的和子类中定义的。</li>
<li>Padding： HotSpot虚拟机的自动内存管理系统要求对象起始地址必须是8字节的整数倍。当对象的实例数据部分没有对齐，用对齐填充补全。所以对齐填充不是必然存在的。</li>
</ol>
</li>
<li><p>为什么压缩指针超过32G就失效</p>
</li>
<li><p>Object o = new Object()在内存中占用了多少个字节</p>
</li>
<li><p>对象怎么定位（直接，间接）</p>
</li>
<li><p>吞吐量优先和响应时间优先垃圾回收器有哪些</p>
</li>
<li><p>CMS的回收流程是什么</p>
</li>
<li><p>CMS的并发预处理和并发可中断预处理是什么</p>
</li>
<li><p>CMS和G1的异同</p>
</li>
<li><p>G1什么时候引发Full Gc</p>
</li>
<li><p>G1两个Reign区不是连续的且可能有可达性的引用,怎么回收</p>
</li>
<li><p>为什么HotSpot不使用C++对象来代表Java对象</p>
</li>
<li><p>Class对象是在堆还是在方法区？</p>
</li>
</ul>
<h1 id="6-参考资料"><a href="#6-参考资料" class="headerlink" title="6. 参考资料"></a>6. 参考资料</h1><ul>
<li><a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/specs/index.html">Java语言和Java虚拟机规范</a></li>
<li><a target="_blank" rel="noopener" href="https://docs.oracle.com/en/java/javase/13/gctuning/introduction-garbage-collection-tuning.html#GUID-326EB4CF-8C8C-4267-8355-21AB04F0D304">Oracle官方垃圾回收文档</a></li>
<li><a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/index.html">Oracle官方垃圾收集器文档</a></li>
<li><a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html">HotSpot 虚拟机官方参数文档</a></li>
<li><a target="_blank" rel="noopener" href="https://www.oracle.com/java/technologies/javase/javase-core-technologies-apis.html">JAVASE核心技术</a></li>
<li><a target="_blank" rel="noopener" href="https://weread.qq.com/web/reader/cf1320d071a1a78ecf19254kc81322c012c81e728d9d180">周志明：深入理解Java虚拟机</a></li>
<li><a target="_blank" rel="noopener" href="https://weread.qq.com/web/reader/53032310717f44515302749kc81322c012c81e728d9d180">彭成寒：JVM:G1源码分析与调优</a></li>
<li><a target="_blank" rel="noopener" href="https://github.com/ddean2009/www.flydean.com/tree/master/cheatSheet">GC参数表-GITHUB版</a></li>
<li><a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot">Oracle官方故障排除指南</a></li>
<li><a target="_blank" rel="noopener" href="https://www.ej-technologies.com/products/jprofiler/overview.html">JProfiler官网</a></li>
<li><a target="_blank" rel="noopener" href="https://alibaba.github.io/arthas/">Arthas用户文档</a></li>
</ul>
</section>
    <!-- Tags START -->
    
      <div class="tags">
        <span>Tags:</span>
        
  <a href="/tags#JVM及并发编程" >
    <span class="tag-code">JVM及并发编程</span>
  </a>

      </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/2020/05/18/%E6%B7%B1%E5%85%A5%E5%88%86%E6%9E%90ArrayList%E6%BA%90%E7%A0%81/">
        <span class="nav-arrow">← </span>
        
          深入分析ArrayList源码
        
      </a>
    
    
      <a class="nav-right" href="/2020/06/18/%E6%B7%B1%E5%85%A5%E5%88%86%E6%9E%90HashMap%E6%BA%90%E7%A0%81/">
        
          深入分析HashMap源码
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">若你觉得我的文章对你有帮助，欢迎点击上方按钮对我打赏</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Utterances START -->
      <div id="utterances"></div>
      <script src="https://utteranc.es/client.js"
        repo="ltyeamin/blogtalks"
        issue-term="pathname"
        theme="github-light"
        crossorigin="anonymous"
        async></script>    
      <!-- Utterances END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
    <strong class="toc-title">Catalog</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#1-GC%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86"><span class="toc-nav-text">1. GC基础知识</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#1-1-%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86"><span class="toc-nav-text">1.1 内存管理</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#1-2-JVM%E5%B8%B8%E8%A7%81%E6%9C%AF%E8%AF%AD"><span class="toc-nav-text">1.2 JVM常见术语</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#1-3-%E5%9E%83%E5%9C%BE%E5%AE%9A%E4%BD%8D%E7%AE%97%E6%B3%95"><span class="toc-nav-text">1.3 垃圾定位算法</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-3-1-%E5%BC%95%E7%94%A8%E8%AE%A1%E6%95%B0%E7%AE%97%E6%B3%95"><span class="toc-nav-text">1.3.1 引用计数算法</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-3-2-%E6%A0%B9%E5%8F%AF%E8%BE%BE%E6%80%A7%E5%88%86%E6%9E%90%E7%AE%97%E6%B3%95"><span class="toc-nav-text">1.3.2 根可达性分析算法</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#1-4-%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E7%AE%97%E6%B3%95%E5%AE%9E%E7%8E%B0"><span class="toc-nav-text">1.4 垃圾回收算法实现</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-4-1-%E6%A0%87%E8%AE%B0%E6%B8%85%E9%99%A4%E7%AE%97%E6%B3%95"><span class="toc-nav-text">1.4.1 标记清除算法</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-4-2-%E5%A4%8D%E5%88%B6%E7%AE%97%E6%B3%95"><span class="toc-nav-text">1.4.2 复制算法</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-4-3-%E6%A0%87%E8%AE%B0%E5%8E%8B%E7%BC%A9%E7%AE%97%E6%B3%95"><span class="toc-nav-text">1.4.3 标记压缩算法</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#1-5-JVM%E5%86%85%E5%AD%98%E5%88%86%E4%BB%A3%E6%94%B6%E9%9B%86%E6%A8%A1%E5%9E%8B"><span class="toc-nav-text">1.5 JVM内存分代收集模型</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#2-%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E5%99%A8"><span class="toc-nav-text">2. 垃圾回收器</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#2-1-Serial%E6%94%B6%E9%9B%86%E5%99%A8"><span class="toc-nav-text">2.1 Serial收集器</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#2-2-Serial-Old%E6%94%B6%E9%9B%86%E5%99%A8"><span class="toc-nav-text">2.2 Serial Old收集器</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#2-3-ParNew%E6%94%B6%E9%9B%86%E5%99%A8"><span class="toc-nav-text">2.3 ParNew收集器</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#2-4-CMS%E6%94%B6%E9%9B%86%E5%99%A8"><span class="toc-nav-text">2.4 CMS收集器</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#2-5-Parallel-Scavenge%E6%94%B6%E9%9B%86%E5%99%A8"><span class="toc-nav-text">2.5 Parallel Scavenge收集器</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#2-6-Parallel-Old%E6%94%B6%E9%9B%86%E5%99%A8"><span class="toc-nav-text">2.6 Parallel Old收集器</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#2-7-G1%E6%94%B6%E9%9B%86%E5%99%A8"><span class="toc-nav-text">2.7 G1收集器</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#2-8-ZGC%E6%94%B6%E9%9B%86%E5%99%A8"><span class="toc-nav-text">2.8 ZGC收集器</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#3-JVM%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0"><span class="toc-nav-text">3. JVM命令行参数</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#3-1-%E6%A0%87%E5%87%86%E5%8F%82%E6%95%B0"><span class="toc-nav-text">3.1 标准参数</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#3-2-%E9%9D%9E%E6%A0%87%E5%87%86%E5%8F%82%E6%95%B0"><span class="toc-nav-text">3.2 非标准参数</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#3-3-%E4%B8%8D%E7%A8%B3%E5%AE%9A%E5%8F%82%E6%95%B0"><span class="toc-nav-text">3.3 不稳定参数</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#3-4-%E5%B8%B8%E7%94%A8%E5%8F%82%E6%95%B0"><span class="toc-nav-text">3.4 常用参数</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#4-%E8%B0%83%E4%BC%98%E5%B7%A5%E5%85%B7%E4%BD%BF%E7%94%A8"><span class="toc-nav-text">4. 调优工具使用</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#4-1-JDK%E8%87%AA%E5%B8%A6"><span class="toc-nav-text">4.1 JDK自带</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#4-1-1-jps%E5%91%BD%E4%BB%A4"><span class="toc-nav-text">4.1.1 jps命令</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#4-1-2-jstat%E5%91%BD%E4%BB%A4"><span class="toc-nav-text">4.1.2 jstat命令</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#4-1-3-jmap%E5%91%BD%E4%BB%A4"><span class="toc-nav-text">4.1.3 jmap命令</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#4-1-4-jinfo%E5%91%BD%E4%BB%A4"><span class="toc-nav-text">4.1.4 jinfo命令</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#4-1-5-jhat%E5%91%BD%E4%BB%A4"><span class="toc-nav-text">4.1.5 jhat命令</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#4-1-6-jstack%E5%91%BD%E4%BB%A4"><span class="toc-nav-text">4.1.6 jstack命令</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#4-1-7-jconsole%E5%91%BD%E4%BB%A4"><span class="toc-nav-text">4.1.7 jconsole命令</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#4-1-8-jvisualvm%E5%91%BD%E4%BB%A4"><span class="toc-nav-text">4.1.8 jvisualvm命令</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#4-2-Arthas"><span class="toc-nav-text">4.2 Arthas</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#4-3-JProfiler"><span class="toc-nav-text">4.3 JProfiler</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#5-%E9%9D%A2%E8%AF%95%E8%BF%9E%E7%8E%AF%E7%82%AE"><span class="toc-nav-text">5. 面试连环炮</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#6-%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="toc-nav-text">6. 参考资料</span></a></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://example.com/2020/05/24/GC调优基础之必备知识/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', '/css/images/error_icon.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== '/css/images/error_icon.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>






    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
  <p class="copyright">
    &copy; 2024 | Proudly powered by <a href="https://hexo.io" target="_blank">Hexo</a>
    <br>
    Theme by <a target="_blank" rel="noopener" href="https://github.com/ltyeamin">tong.li</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("https://cdn.staticfile.org/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("https://cdn.staticfile.org/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      hljs.configure({useBR: true});
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>
<!-- Baidu Tongji -->



<script src='https://cdn.staticfile.org/mermaid/8.11.2/mermaid.min.js'></script>



<script src="/js/script.js"></script>


  </body>
</html>